package com.hqyj.erpserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hqyj.erpserver.dao.entity.SysUser;
import com.hqyj.erpserver.service.ISysUserService;
import com.hqyj.erpserver.util.JWTUtil;
import com.hqyj.erpserver.util.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HQYJ
 * @since 2021-10-26
 */
@RestController
@RequestMapping("/sysUser")
public class SysUserController {
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private JWTUtil jwtUtill;

    @RequestMapping("/login")
    public RestResult<SysUser> login(SysUser sysUser){
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", sysUser.getUserName());
        wrapper.eq("user_pwd",sysUser.getUserPwd());
        SysUser user = sysUserService.getOne(wrapper);
        if(user == null){//登录失败
            return RestResult.error("用户名或密码不正确");
        }else{
            String token = jwtUtill.createJwt(user);
            return RestResult.ok(token);
        }
    }
}

